<?php

/*
 * This file is part of the Geek-Zoo Projects.
 *
 * @copyright (c) 2010 Geek-Zoo Projects More info http://www.geek-zoo.com
 * @license http://opensource.org/licenses/gpl-2.0.php The GNU General Public License
 * @author quqiang <quqiang@geek-zoo.com>
 *
 */
require ROOT_PATH.'/Helper/admin_abstract.php';

class Action extends admin_abstract
{
    function index()
    {

	    $q = _GET('q');
	    
	    if(addslashes($q)){
		 
		    $like = " where name LIKE '%$q%' ";
	    }else{
	    	
		    $like = '';
	    }
	    // print_r($this->info);当前登录用户信息
	    $t=_GET('t'); 
	    if(!$t ){
	    	$t = 'desc';
	    }
	    if($t=='DESC'){
	    	$t = 'asc';
	    }
	    if($t=='ASC'){
	    	$t = 'desc';
	    }
	$data = _model('user')->getList($like.'order by add_time '.$t, new Pager(20));
	$this->view->assign($data);    
        $this->view->display("admin/user_list.html");
    }

    function add()
    {
    	$this->view->display("admin/user_add.html");
    }

    function insert()
    {
    	
	    $info = _POST('info', array());

	    
	    if(empty($info['name']) || empty($info['password']) || empty($info['email'])){
		$this->msg('请填写密码!');	    
	    }

	    $check_name= _model('user')->read(array('name'=>$info['name']));
	    $check_email= _model('user')->read(array('email'=>$info['email']));
	
	    if($check_name){
	    
	    	$this->msg('用户名已存在!');
	    }

	    if($check_email){

		 $this->msg('email已存在!');
	    }
	    $insert = array();
	    $insert['name'] = $info['name'];
	    $insert['is_admin'] = 0;
	    $insert['password'] = md5($info['password']);
	    $insert['email'] = $info['email'];
	    $insert['add_time'] = time();
	    $id = _model('user')->create($insert);
    		
	    if($id){
	    
	    	$this->msg('添加成功', 'admin/user_manage.html');
	    }else{
	    
	    	$this->msg('添加失败');
	    }
	    
    }

    function mod()
    {

	   $id = _GET('id', 0);
		if($id){
			$msg = _model('user')->read(array('id'=>$id));	
			$this->view->assign('msg', $msg);
		}
	   	$this->view->display('admin/user_mod.html');
		    
    
    
    }

    function update()
    {
		
	    
	    $info = _POST('info', array());
	    	
	    $id= _POST('id', 0);
	    if(!$id){
		$this->msg('参数不全');	    
	    	
	    }
	    if(empty($info['name']) || empty($info['email'])){
		$this->msg('信息不完整');	    
	    }
	
    	    if (filter_var($info['email'], FILTER_VALIDATE_EMAIL) === false) {

		$this->msg('请输入正确的邮箱!');    
	    }   

	    $check_name= _model('user')->read(array('name'=>$info['name']));
	    $check_email= _model('user')->read(array('email'=>$info['email']));
	
	    if($check_name && $id!=$check_name['id']){
	    
	    	$this->msg('用户名已存在!');
	    }

	    if($check_email && $id!=$check_email['id']){

		 $this->msg('email已存在!');
	    }
	    
	    	$insert = array();
	    	$insert['name'] = $info['name'];
		$insert['is_admin'] = 0;
	
		if(!empty($info['password'])){
			$insert['password'] = md5($info['password']);
		}		
		$insert['email'] = $info['email'];
	    	$insert['add_time'] = time();
	    	_model('user')->update(array('id'=>$id), $insert);
	    	$this->msg('修改成功', 'admin/user_manage.html');

	    
	    
    }

    function delete()
    {

	    $id = _GET('id', 0);
	    if($id){
	   _model('user')->delete(array('id'=>$id));
	
	    }
	    $this->redirect('admin/user_manage.html');
    
    
    }

     function alldel(){
	    $arr = _POST('ids', array());

	    foreach($arr as $val){
	    
		    _model('user')->delete(array('id'=>$val));
	    }
	    $this->redirect('admin/user_manage.html');
    }

    
	   

}



?>
